package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Int;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class Spark53_Operate_KV {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        //Tuple元组，是一个kv对
        final Tuple2<String,Integer> a = new Tuple2<>("a",1);
        final Tuple2<String,Integer> a1 = new Tuple2<>("b",2);
        final Tuple2<String,Integer> a2 = new Tuple2<>("c",3);
        final List<Tuple2<String,Integer>> tuple2 = Arrays.asList(a,a1,a2);
        /*
        这样做还是仍然把Tuple当做整体处理，并没有对kv提出来处理
        final JavaRDD<Tuple2<String,Integer>> rdd = jsc.parallelize(tuple2);
        rdd.map(
                t -> new Tuple2<>(t._1,t._2 * 2)
        ).collect().forEach(System.out::println);
        */

        //这里就是把kv单独提出来进行处理
        final JavaPairRDD<String, Integer> pairRDD = jsc.parallelizePairs(tuple2);
        pairRDD.mapValues(
                num -> num * 2
        ).collect().forEach(System.out::println);
        jsc.close();
    }
}
